/*****
author : searchNode
date: 2016/12/10
******/
import dataC from './data'
import tools from './tools'
export default {
  
  getLine (val, label, flag, cFlag) {
    var ll = ''
    var labelArr = ''
  
    if (val.toString() === label) return true
    if (cFlag) return false
    var cNumber = Number(label.substr(1))
    var abc = /^[\x00-\xff]/.test(label.substr(0, 1))
    //    cNumber = Number(cNumber)

    if (cNumber === Number(val)) {
      if (abc) return
    }

    if (flag === 's') {
      labelArr = label.split('/')
      ll = labelArr[0]
      
      if (val.toString() === ll) {
        return true
      }

      labelArr = label.split('-')
      ll = labelArr[0]
      
      if (val.toString() === ll) {
        return true
      }   
    } else {
      labelArr = label.split('-')
      ll = labelArr[1]  
    }
  
    if (labelArr[1]) {
      if (val.toString() === ll) {
        return true
      }  
    }

    return false
  },

  traverse (id, port, line) {
    var node = tools.getNode(dataC.nodes, id)
    
    if (node.type === 'distributing_cabinet') {
      var edg = this.findT(port, id, true, line.data.id)
      // this.addLine(arr)
      edg['group'] = 'edges'
      dataC.cy.add(edg)
    }  
  },

  findT (val, id, cFlag, lineId) {
    var arr = []
    
  // this.delAllEdge()
    if (!lineId) return
    if (dataC.edges) {
      for (var i=0; i<dataC.edges.length; i++) {
        var line = dataC.edges[i]
        var source = line['data']['source'].toString()
        var target = line['data']['target'].toString()
        var sPort = line['data']['s_port_serial']
        var ePort = line['data']['e_port_serial']
        if (source === id.toString()) {
          if (this.getLine(val, sPort, 's', cFlag)) {
            if (lineId.toString() !== line.data['id'].toString()) {
              this.traverse(target, ePort, line)
              return line
            }  
          }
        }
      
        if (target === id.toString()) {
          if (this.getLine(val, ePort, 'e', cFlag)) {
            if (lineId.toString() !== line.data['id'].toString()) {
              this.traverse(source, sPort, line)
              return line
            } 
          }
        }
      }
    }

    return arr
  },

  find (val, id) {
    var arr = []
    
  // this.delAllEdge()

    if (dataC.edges) {
      for (var i=0; i<dataC.edges.length; i++) {
        // console.log('start:', dataC.edges[i])
        var line = dataC.edges[i]
        var source = line['data']['source'].toString()
        var target = line['data']['target'].toString()
        var sPort = line['data']['s_port_serial']
        var ePort = line['data']['e_port_serial']
        if (source === id.toString()) {
          if (this.getLine(val, sPort, 's')) {
            arr.push(line)
            this.traverse(target, ePort, line)
          }
        }
      
        if (target === id.toString()) {
          if (this.getLine(val, ePort, 'e')) {
            arr.push(line)
            this.traverse(source, sPort, line)
          }
        }
      }
    }

    return arr
  },

  filter_ (that, val) {
    var id = dataC.master.id
    var url = '/topology/getLinePort/'
    fetchJson.getJson({
      self: this,
      url: url + '?id1=' + data.s_select.data.id + '&id2=' + data.e_select.data.id,
      callback: this.callback,
      error: this.error
    })
  },

  addLine (arr) {
    if (arr.length > 0) {
      dataC.cy.add(arr)
    } else {
      that.$message('没有链接线')
    }
  },

  filter (that, val) {
   // alert(dataC.master)
    var id = dataC.master.id
    this.delAllEdge()
    // for (var i=0; i < dataC.nodes.length; i++) {
    //  var id = dataC.nodes[i].data['id']
    var arr = this.find(val, id)
    this.addLine(arr)
    // } 
  },

  delEdge (id) {
    dataC.cy.edges(function (i, edge) {
      var data = edge.data()
      if (data.id === id) {
        dataC.cy.add(edge)
      }
    }) 
  },

  delAllEdge (id) {
    dataC.cy.edges(function (i, edge) {
      dataC.cy.remove(edge)
    }) 
  },

  doubleClick (data) {
  },

  setEl (id, flag) {
    dataC.cy.nodes(function (i, node) {
      var data = node.data()

      if (flag) {
        if (!id) {
       // node.removeClass('eater')
        } else {
          if (id.toString() === data['id'].toString()) {
            node.addClass('eater')
          } 
        }
      } else {
        console.log('kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk:', id)
        if (!id) {
       // node.removeClass('eater')
        } else {
          if (id.toString() === data['id'].toString()) {
            node.removeClass('eater')
            console.log('kkkkkkkkkkklllllllllll:', id)
          } 
        }
      }   
    })
  }
}
